package org.jeecg.modules.purchase.service.impl;

import org.jeecg.modules.purchase.entity.MerchantProvider;
import org.jeecg.modules.purchase.entity.MerchantProviderCertification;
import org.jeecg.modules.purchase.mapper.MerchantProviderCertificationMapper;
import org.jeecg.modules.purchase.mapper.MerchantProviderMapper;
import org.jeecg.modules.purchase.service.IMerchantProviderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 供应商
 * @Author: jeecg-boot
 * @Date:   2022-12-21
 * @Version: V1.0
 */
@Service
public class MerchantProviderServiceImpl extends ServiceImpl<MerchantProviderMapper, MerchantProvider> implements IMerchantProviderService {

	@Autowired
	private MerchantProviderMapper merchantProviderMapper;
	@Autowired
	private MerchantProviderCertificationMapper merchantProviderCertificationMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(MerchantProvider merchantProvider, List<MerchantProviderCertification> merchantProviderCertificationList) {
		merchantProviderMapper.insert(merchantProvider);
		if(merchantProviderCertificationList!=null && merchantProviderCertificationList.size()>0) {
			for(MerchantProviderCertification entity:merchantProviderCertificationList) {
				//外键设置
				entity.setProviderId(merchantProvider.getId());
				entity.setProviderCode(merchantProvider.getCode());
				merchantProviderCertificationMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(MerchantProvider merchantProvider,List<MerchantProviderCertification> merchantProviderCertificationList) {
		merchantProviderMapper.updateById(merchantProvider);
		
		//1.先删除子表数据
		merchantProviderCertificationMapper.deleteByMainId(merchantProvider.getId());
		
		//2.子表数据重新插入
		if(merchantProviderCertificationList!=null && merchantProviderCertificationList.size()>0) {
			for(MerchantProviderCertification entity:merchantProviderCertificationList) {
				//外键设置
				entity.setProviderId(merchantProvider.getId());
				entity.setProviderCode(merchantProvider.getCode());
				merchantProviderCertificationMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		merchantProviderCertificationMapper.deleteByMainId(id);
		merchantProviderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			merchantProviderCertificationMapper.deleteByMainId(id.toString());
			merchantProviderMapper.deleteById(id);
		}
	}
    
    @Override
    public List<MerchantProvider> getExpried(String expiredDay) {
        return merchantProviderMapper.getExpried(expiredDay);
    }
    
    /**
     * 获取指定合同结束日期的数据
     * @param expriedDay
     * @return
     */
    @Override
    public List<MerchantProvider> getByEndTime(String expriedDay) {
        return merchantProviderMapper.getByEndTime(expriedDay);
    }
    
    @Override
    public Boolean expriedUpdate(String id) {
        return merchantProviderMapper.expriedUpdate(id);
    }
}
